package org.itboys.core.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayContactMessage;
import org.itboys.core.util.NumberUtil;
import org.itboys.core.util.PhoneUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/20
 * Description:
 */

@Service
public class SundayContactMessageService extends BaseMongoService<SundayContactMessage> {
    @Autowired
    private SundayUploadService uploadService;

    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }
    @Override
    protected Class<SundayContactMessage> getEntityClass() {
        return SundayContactMessage.class;
    }

    /**
     * 分页查询所有
     * @param request
     * @return
     */
    public PageResult<SundayContactMessage> select(HttpServletRequest request){
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        //等待加入条件
        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);

        PageResult<SundayContactMessage> result = super.containpageQuery(request,vagueParam,param);

        return result;
    }

    /**
     * 保存联系客服内容
     * @param id
     * @param message
     * @param imageFile
     * @throws Exception
     */
    public void save(Long id,SundayContactMessage message,MultipartFile imageFile)throws Exception{
        Map<String, String> map = uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(map.get(SundayUploadService.UPLOAD_HALF_PATH))){
            message.setImage(map.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        if(LongUtil.isNotZreo(id)){
            super.updateExceptEmpty(id,message);

        }else{
            message.setStatus(0);
            super.save(message);
        }
    }

    /**
     * 根据id查找消息中心
     * @param id
     * @return
     */
    public SundayContactMessage findOne(Long id){
        SundayContactMessage message = new SundayContactMessage();
        if (LongUtil.isNotZreo(id)){
            return super.getById(id);
        }else return message;
    }

    /**
     * 设置状态
     * @param id
     * @param status
     */
    public void setStatus(Long id, Integer status) {
        SundayContactMessage message = super.getById(id);
        if (LongUtil.isNotZreo(message.getUserId())) {
            super.update(id, "status", status);
        }else {
            throw new ErrorException();
        }
    }
}
